package slimeVolleyball;

import javax.swing.*;
import java.awt.event.*;
import java.util.HashSet;
import java.util.Set;

public class Slime implements ActionListener
{
	int x;
	int y;
	int w;
	int h;
	int strtA;
	int arcA;
	int keyPressed;
	int player;
	SlimeGui gui;
	Timer timer;
	int counter = 1;
	int color;
	Boolean moveLeft;
	Boolean jump;
	Set<Integer> keysPressed;
	Net net;

	public Slime(int x, int y, int w, int h, int strtA, int arcA, int player, SlimeGui gui, int color, Boolean moveLeft, Boolean jump, Net net)
	{
		this.x=x;
		this.y=y;
		this.w=w;
		this.h=h;
		this.strtA=strtA;
		this.arcA=arcA;
		this.player=player;
		this.gui = gui;
		timer = new Timer(10, this);
		this.color = color;
		this.moveLeft = moveLeft;
		this.jump = jump;
		keysPressed = new HashSet<Integer>();
		this.net = net;
	}
	public int slimeCoordX()
	{
		return this.x;
	}
	public int slimeCoordY()
	{
		return this.y;
	}
	public int slimeWidth()
	{
		return this.w;
	}
	public int slimeHeight()
	{
		return this.h;
	}
	public int whichPlayer()
	{
		return this.player;
	}
	public void moveSlimeX(int speed, Boolean forward)
	{
		if(forward)
		{
			this.x+=speed;
			gui.update();
		}
		else
		{
			this.x-=speed;
			gui.update();
		}
	}
	public void jumpSlimeUp()
	{
		y-=2;
		gui.update();
	}
	public void jumpSlimeDown()
	{
		y+=2;
		gui.update();

	}
	public void triggerJump()
	{
		jump = true;
	}
	public void startTimer()
	{
		timer.start();
	}
	public void stopTimer()
	{
		timer.stop();
	}

	public void actionPerformed(ActionEvent arg0) 
	{
		if(jump)
		{
			if(counter<25)
			{
				jumpSlimeUp();
				counter++;
			}
			else if(counter<50)
			{
				jumpSlimeDown();
				counter++;
			}
			else
			{
				//timer.stop();
				counter = 0;
				jump = false;
			}
		}
		if(keysPressed.contains(KeyEvent.VK_W)&&player == 1)
		{
			triggerJump();
		}
		else if(keysPressed.contains(KeyEvent.VK_S)&&player == 1)
		{
			if(slimeColor()<4)
			{
				changeColor();
			}
			else
			{
				resetColor();
			}
		}

		if(keysPressed.contains(KeyEvent.VK_A)&&player == 1&&slimeCoordX()>0)
		{
			moveSlimeX(4, false);
		}
		else if(keysPressed.contains(KeyEvent.VK_D) && player == 1&&(slimeCoordX()+slimeWidth())<395)
		{
			moveSlimeX(4, true);;
		}

		if(keysPressed.contains(KeyEvent.VK_UP) && player == 2)
		{
			triggerJump();
		}

		else if(keysPressed.contains(KeyEvent.VK_DOWN)&&player == 2)
		{
			if(slimeColor()<4)
			{
				changeColor();
			}
			else
			{
				resetColor();
			}
		}
		if(keysPressed.contains(KeyEvent.VK_LEFT)&&player == 2&&slimeCoordX()>405)
		{
			moveSlimeX(4, false);

		}
		else if(keysPressed.contains(KeyEvent.VK_RIGHT)&&player == 2&&(slimeCoordX()+slimeWidth())<800)
		{
			moveSlimeX(4, true);
		}
	}

	public int slimeColor()
	{
		return color;
	}
	public void resetColor()
	{
		color = 1;
		gui.update();
	}
	public void changeColor()
	{
		color++;
		gui.update();
	}
	public Set<Integer> getKeyPressed()
	{
		return keysPressed;
	}
	public void setKeyPressed(int code)
	{
		keysPressed.add(code);
	}
	public void removeKeyPressed(int code)
	{
		keysPressed.remove(code);
	}
}
